<template>
	<view class="container">
		<!-- 导航栏 -->
		<view class="nav-bar">
			<view class="status-bar"></view>
			<view class="nav-content">
				<view class="nav-left" @click="goBack">
					<text class="back-icon">←</text>
				</view>
				<view class="nav-title">我的优惠券</view>
				<view class="nav-right">
					<text class="more-icon">⋯</text>
					<!-- <text class="dash-icon">—</text> -->
					<text class="target-icon">◎</text>
				</view>
			</view>
		</view>

		<!-- 选项卡 -->
		<view class="tab-section">
			<view class="tab-container">
				<view 
					class="tab-item" 
					:class="{ active: currentTab === 'delivery' }"
					@click="switchTab('delivery')"
				>
					<text class="tab-text">外卖券</text>
					<view class="tab-line" v-if="currentTab === 'delivery'"></view>
				</view>
				<view 
					class="tab-item" 
					:class="{ active: currentTab === 'mall' }"
					@click="switchTab('mall')"
				>
					<text class="tab-text">商城券</text>
					<view class="tab-line" v-if="currentTab === 'mall'"></view>
				</view>
			</view>
		</view>

		<!-- 优惠券列表 -->
		<view class="coupon-list" v-if="currentCoupons.length > 0">
			<view 
				class="coupon-item" 
				v-for="(coupon, index) in currentCoupons" 
				:key="index"
				:class="{ expired: coupon.status === 'expired', used: coupon.status === 'used' }"
				@click="useCoupon(coupon)"
			>
				<view class="coupon-left">
					<view class="coupon-amount">
						<text class="amount-symbol">¥</text>
						<text class="amount-value">{{ coupon.amount }}</text>
					</view>
					<view class="coupon-condition">{{ coupon.condition }}</view>
				</view>
				<view class="coupon-right">
					<view class="coupon-title">{{ coupon.title }}</view>
					<view class="coupon-desc">{{ coupon.description }}</view>
					<view class="coupon-time">
						有效期：{{ coupon.validUntil }}
					</view>
				</view>
				<view class="coupon-status">
					<text v-if="coupon.status === 'available'" class="status-available">可使用</text>
					<text v-if="coupon.status === 'used'" class="status-used">已使用</text>
					<text v-if="coupon.status === 'expired'" class="status-expired">已过期</text>
				</view>
			</view>
		</view>

		<!-- 空状态 -->
		<view class="empty-state" v-else>
			<view class="empty-icon">
				<text class="icon-text">📄</text>
				<text class="icon-magnifier">🔍</text>
				<text class="icon-plus">+</text>
				<text class="icon-circle">o</text>
				<text class="icon-dash">-</text>
			</view>
			<text class="empty-text">{{ getEmptyText() }}</text>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			currentTab: 'delivery', // delivery: 外卖券, mall: 商城券
			deliveryCoupons: [], // 外卖券列表
			mallCoupons: [] // 商城券列表
		}
	},
	computed: {
		currentCoupons() {
			return this.currentTab === 'delivery' ? this.deliveryCoupons : this.mallCoupons;
		}
	},
	onLoad() {
		this.getCoupons()
	},
	methods: {
		goBack() {
			uni.navigateBack()
		},
		
		// 切换选项卡
		switchTab(tab) {
			this.currentTab = tab
		},
		
		// 获取优惠券数据
		async getCoupons() {
			try {
				// 这里可以调用API获取真实数据
				// const response = await this.$api.getCoupons()
				// this.deliveryCoupons = response.data.delivery
				// this.mallCoupons = response.data.mall
				
				// 模拟外卖券数据
				this.deliveryCoupons = [
					{
						id: 1,
						title: '满减优惠券',
						description: '适用于指定商家',
						amount: 15,
						condition: '满50可用',
						validUntil: '2024-12-31',
						status: 'available'
					},
					{
						id: 2,
						title: '新用户专享',
						description: '首单立减',
						amount: 20,
						condition: '满80可用',
						validUntil: '2024-11-30',
						status: 'available'
					},
					{
						id: 3,
						title: '周末特惠',
						description: '周六日可用',
						amount: 10,
						condition: '满30可用',
						validUntil: '2024-10-15',
						status: 'expired'
					},
					{
						id: 4,
						title: '生日福利',
						description: '生日月专享',
						amount: 25,
						condition: '满100可用',
						validUntil: '2024-09-30',
						status: 'used'
					}
				];
				
				// 模拟商城券数据
				this.mallCoupons = [
					{
						id: 5,
						title: '商城通用券',
						description: '全场通用',
						amount: 30,
						condition: '满200可用',
						validUntil: '2024-12-25',
						status: 'available'
					},
					{
						id: 6,
						title: '电子产品券',
						description: '仅限电子产品',
						amount: 50,
						condition: '满500可用',
						validUntil: '2024-11-20',
						status: 'available'
					},
					{
						id: 7,
						title: '服装专用券',
						description: '服装类商品专用',
						amount: 40,
						condition: '满300可用',
						validUntil: '2024-10-31',
						status: 'expired'
					}
				];
				
			} catch (error) {
				console.error('获取优惠券失败:', error)
			}
		},
		
		// 使用优惠券
		useCoupon(coupon) {
			if (coupon.status === 'available') {
				uni.showToast({
					title: `使用${coupon.title}`,
					icon: 'none'
				});
				// 这里可以跳转到使用页面或调用使用API
			} else if (coupon.status === 'expired') {
				uni.showToast({
					title: '优惠券已过期',
					icon: 'none'
				});
			} else if (coupon.status === 'used') {
				uni.showToast({
					title: '优惠券已使用',
					icon: 'none'
				});
			}
		},
		
		// 获取空状态文本
		getEmptyText() {
			return this.currentTab === 'delivery' ? '暂无外卖券' : '暂无商城券'
		}
	}
}
</script>

<style lang="scss">
.container {
	background: #f5f5f5;
	min-height: 100vh;
}

/* 导航栏 */
.nav-bar {
	background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
	position: relative;
}

.status-bar {
	height: calc(var(--status-bar-height, 0) + 20rpx);
	background: transparent;
}

.nav-content {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 20rpx 30rpx;
}

.nav-left, .nav-right {
	display: flex;
	align-items: center;
	gap: 20rpx;
}

.back-icon {
	font-size: 48rpx;
	color: white;
	font-weight: bold;
}

.nav-title {
	font-size: 36rpx;
	font-weight: 700;
	color: white;
}

.nav-right {
	gap: 15rpx;
}

.more-icon, .dash-icon, .target-icon {
	font-size: 32rpx;
	color: white;
}

/* 选项卡 */
.tab-section {
	background: white;
	border-bottom: 1rpx solid #f0f0f0;
}

.tab-container {
	display: flex;
	padding: 0 30rpx;
}

.tab-item {
	flex: 1;
	display: flex;
	flex-direction: column;
	align-items: center;
	padding: 30rpx 0;
	position: relative;
}

.tab-text {
	font-size: 32rpx;
	color: #999;
	font-weight: 500;
	transition: all 0.3s ease;
}

.tab-item.active .tab-text {
	color: #007AFF;
	font-weight: 600;
}

.tab-line {
	position: absolute;
	bottom: 0;
	left: 50%;
	transform: translateX(-50%);
	width: 60rpx;
	height: 4rpx;
	background: #007AFF;
	border-radius: 2rpx;
}

/* 优惠券列表 */
.coupon-list {
	padding: 30rpx;
}

.coupon-item {
	display: flex;
	background: white;
	border-radius: 16rpx;
	margin-bottom: 20rpx;
	padding: 30rpx;
	box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	position: relative;
	overflow: hidden;
}

.coupon-item::before {
	content: '';
	position: absolute;
	left: 0;
	top: 0;
	bottom: 0;
	width: 8rpx;
	background: #ff6600;
}

.coupon-item.expired::before {
	background: #ccc;
}

.coupon-item.used::before {
	background: #999;
}

.coupon-left {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	min-width: 160rpx;
	padding-right: 30rpx;
	border-right: 2rpx dashed #eee;
}

.coupon-amount {
	display: flex;
	align-items: baseline;
	margin-bottom: 10rpx;
}

.amount-symbol {
	font-size: 28rpx;
	color: #ff6600;
	font-weight: bold;
}

.amount-value {
	font-size: 48rpx;
	color: #ff6600;
	font-weight: bold;
}

.coupon-condition {
	font-size: 24rpx;
	color: #999;
}

.coupon-right {
	flex: 1;
	padding-left: 30rpx;
	display: flex;
	flex-direction: column;
	justify-content: center;
}

.coupon-title {
	font-size: 32rpx;
	color: #333;
	font-weight: 600;
	margin-bottom: 8rpx;
}

.coupon-desc {
	font-size: 26rpx;
	color: #666;
	margin-bottom: 12rpx;
}

.coupon-time {
	font-size: 24rpx;
	color: #999;
}

.coupon-status {
	position: absolute;
	top: 20rpx;
	right: 20rpx;
}

.status-available {
	font-size: 24rpx;
	color: #007AFF;
	background: rgba(0, 122, 255, 0.1);
	padding: 4rpx 12rpx;
	border-radius: 12rpx;
}

.status-used {
	font-size: 24rpx;
	color: #999;
	background: #f5f5f5;
	padding: 4rpx 12rpx;
	border-radius: 12rpx;
}

.status-expired {
	font-size: 24rpx;
	color: #ff4757;
	background: rgba(255, 71, 87, 0.1);
	padding: 4rpx 12rpx;
	border-radius: 12rpx;
}

/* 过期和已使用状态样式 */
.coupon-item.expired {
	opacity: 0.6;
}

.coupon-item.expired .amount-symbol,
.coupon-item.expired .amount-value {
	color: #ccc;
}

.coupon-item.used {
	opacity: 0.6;
}

.coupon-item.used .amount-symbol,
.coupon-item.used .amount-value {
	color: #999;
}

/* 空状态 */
.empty-state {
	flex: 1;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 120rpx 60rpx;
	background: white;
	margin: 30rpx;
	border-radius: 16rpx;
}

.empty-icon {
	position: relative;
	margin-bottom: 40rpx;
}

.icon-text {
	font-size: 120rpx;
	color: #ccc;
	display: block;
	transform: rotate(-5deg);
}

.icon-magnifier {
	position: absolute;
	bottom: 20rpx;
	right: 20rpx;
	font-size: 40rpx;
	color: #999;
}

.icon-plus {
	position: absolute;
	top: 10rpx;
	left: 10rpx;
	font-size: 24rpx;
	color: #999;
	font-weight: bold;
}

.icon-circle {
	position: absolute;
	top: 30rpx;
	right: 10rpx;
	font-size: 20rpx;
	color: #999;
}

.icon-dash {
	position: absolute;
	bottom: 10rpx;
	left: 30rpx;
	font-size: 20rpx;
	color: #999;
}

.empty-text {
	font-size: 32rpx;
	color: #999;
	font-weight: 500;
}
</style>
